#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlDaDeskAccountMapping : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        string m_startDefault;
        DADesk_PDA_Service m_daDeskService;

        public UserControlDaDeskAccountMapping(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            labelTitle.Text = name;
            this.Name = name;
            m_current_user = currentUser;
            m_daDeskService = new DADesk_PDA_Service();
            m_mainForm = mainForm;
            //initColumnWidth();
            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
            listViewAlert.DoubleClick += new EventHandler(listViewAlert_DoubleClick);
            toolBarButtonRefresh_Click(null, null);
        }

        //private void initColumnWidth()
        //{
        //    listViewAlert.Columns[0].Width = 100;
        //    listViewAlert.Columns[1].Width = 72;
        //    listViewAlert.Columns[2].Width = 72;
        //    listViewAlert.Columns[3].Width = 100;
        //    listViewAlert.Columns[4].Width = 44;
        //    listViewAlert.Columns[5].Width = 79;
        //    listViewAlert.Columns[6].Width = 83;
        //    listViewAlert.Columns[7].Width = 83;
        //    listViewAlert.Columns[8].Width = 100;
        //    listViewAlert.Columns[9].Width = 100;
        //    listViewAlert.Columns[10].Width = 100;
        //    listViewAlert.Columns[11].Width = 50;
        //}

        void listViewAlert_DoubleClick(object sender, EventArgs e)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            DaDeskAccount a = (DaDeskAccount)listViewAlert.SelectedItem.Tag;
            DADeskAccountForm adf = new DADeskAccountForm(a, this);
            adf.ShowDialog();
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewAlert.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewAlert.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewAlert.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewAlert.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewAlert.Items.Clear();
            statusBar1.Text = "Total : 0 item(s)";
            IList companyMasters = m_daDeskService.GetAllAccountMapping();
            LoadItems(companyMasters);
        }

        private void LoadItems(IList companyMasters)
        {

            foreach (DaDeskAccount a in companyMasters)
            {
                ListViewItem item = listViewAlert.Items.Add(a.AccountCode);
                item.SubItems.Add(a.AgrAccountCode);
                item.Tag = a;
            }
            statusBar1.Text = string.Format("Total : {0} item(s)", companyMasters.Count);
        }

        internal void UpdateSelectedItem(DaDeskAccount company)
        {
            if (listViewAlert.SelectedItem == null) return;
            if (listViewAlert.SelectedItem.Tag == null) return;
            listViewAlert.SelectedItem.SubItems[1].Text = company.AgrAccountCode;
            listViewAlert.SelectedItem.Tag = company;
        }
    }
}